﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DevTools.ClassStuffGenerator.Components
{
    public class Item
    {
        private Item()
        {
            Fields = new List<Item>();
        }

        public static Item Create(Type classType, FieldInfo fieldInfo, Item @clsss)
        {
            Item item = new Item();

            item.ClassName = classType.Name;
            item.ClassFullName = classType.FullName;
            item.ClassNamespace = classType.Namespace;

            if (fieldInfo != null)
            {
                item.FieldName = fieldInfo.FieldType.Name;
                item.FieldFullName = fieldInfo.FieldType.FullName;
                item.FieldNamespace = fieldInfo.FieldType.Namespace;
                item.FieldInstanceName = fieldInfo.Name;
            }

            if (@clsss != null)
                @clsss.Fields.Add(item);

            return item;
        }

        public List<Item> Fields { get; set; }

        public string ClassName { get; set; }
        public string ClassFullName { get; set; }
        public string ClassNamespace { get; set; }
        
        public string FieldName { get; set; }
        public string FieldFullName { get; set; }
        public string FieldNamespace { get; set; }
        
        public string FieldInstanceName { get; set; }
        public string FieldModifier { get; set; }

        private string GeneateCodeCore(string code, char casing, string macro)
        {
            char firstChar;
            
            if (casing == 'N')
                firstChar = code[0];
            else if (casing == 'P')
                firstChar = char.ToUpper(code[0]);
            else if (casing == 'C')
                firstChar = char.ToLower(code[0]);
            else
                throw new Exception(string.Format("Wrong Casing : {0} in {1}", casing, macro));

            return firstChar + code.Substring(1);
        }

        public string GeneateCode(string macro)
        {
            char @type = macro[0];
            char @property = macro[1];
            char casing = macro[2];

            if (@type == 'C')
            {
                switch (@property)
                {
                    case 'N':
                        return GeneateCodeCore(ClassName, casing, macro);
                    case 'F':
                        return GeneateCodeCore(ClassFullName, casing, macro);
                    case 'S':
                        return GeneateCodeCore(ClassNamespace, casing, macro);
                    default:
                        throw new Exception(string.Format("Wrong Property : {0} in {1}", @property, macro));
                }
            }

            else if (@type == 'F')
            {
                switch (@property)
                {
                    case 'N':
                        return GeneateCodeCore(FieldName, casing, macro);
                    case 'F':
                        return GeneateCodeCore(FieldFullName, casing, macro);
                    case 'S':
                        return GeneateCodeCore(FieldNamespace, casing, macro);
                    case 'I':
                        return GeneateCodeCore(FieldInstanceName, casing, macro);
                    case 'M':
                        return GeneateCodeCore(FieldModifier, casing, macro);
                    default:
                        throw new Exception(string.Format("Wrong Property : {0} in {1}", @property, macro));
                }
            }

            else
            {
                throw new Exception(string.Format("Wrong Type : {0} in {1}", @type, macro));
            }
        }
    }
}
